<?php

include_once '../dao/Distributor.php';
include_once '../dao/DistributorDAO.php';

if(isset ($_POST['function']))
	uploadFile();
else 
	downloadFile();
	
	
function uploadFile() {
	$fileName = $_FILES['file']['name'];
	$tmpName  = $_FILES['file']['tmp_name'];
	$fileSize = $_FILES['file']['size'];
	$fileType = $_FILES['file']['type'];
	
	$fh = fopen($tmpName, 'r');
	$data = fgetcsv($fh, 1024);//read the first line, which will be the header
	
	set_time_limit(0);
	while(!feof($fh)) { 
		$data = fgetcsv($fh, 1024);
		$distributor = new Distributor($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8]);
	
		$distributorDAO = DistributorDAO::getInstance();
		if($distributor->getPackNo() != "")
			$distributorDAO->insertRecord($distributor);
		
	}
	fclose($fh);
	
}

function downloadFile() {
	$distributorDAO = DistributorDAO::getInstance();
	$distributorDAO->downloadFile();
}

?>